/**
 * 
 */
package checkip;

import it.unipr.ce.dsg.s2p.message.BasicMessage;
import it.unipr.ce.dsg.s2p.message.Payload;
import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.GregorianCalendar;
import java.util.HashMap;

/**
 * @author Fabio Melillo
 * 
 */
public class StartSimpleTestPeer {
	
	private static final String SUPER_PEER = "193.206.108.143:2222";

	private static final String KEY = String.valueOf(GregorianCalendar
			.getInstance().getTimeInMillis());

	public static void main(String[] args) {

		if (args.length < 2) {
			System.out
					.println("PARAMETRI OBBLIGATORI tuoNomePeer porta name@SBCip:SBCport TIPO");
			System.exit(1);
		} else {
			// SimpleTestPeer testPeer = new SimpleTestPeer(null, KEY, args[0],
			// Integer.parseInt(args[1]));
			SimpleTestPeer testPeer = new SimpleTestPeer(null, KEY, args[0],
					Integer.parseInt(args[1]));
			// GatewayPeer gatewayPeer=new GatewayPeer(args[0],
			// Integer.parseInt(args[1]));

			String address, name, ip, port;
			address = args[2];
			// int at = address.indexOf("@");
			// int index = address.indexOf(":");
			// name = address.substring(0, at);
			// ip = address.substring(at + 1, index);
			// port = address.substring(index + 1);

			if (args.length>3) {

				testPeer.setSbc(address);
				System.out.println("SBC address: " + args[2]);
				testPeer.checkNAT();
//				testPeer.requestPublicAddress();
				
			} else {
				int at = address.indexOf("@");
				int index = address.indexOf(":");
				name = address.substring(0, at);
				ip = address.substring(at + 1, index);
				port = address.substring(index + 1);
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("IP_DEST", ip);
				map.put("PORT_DEST", port);
				map.put("Tipo Invio", "Peer normale che invia all'indirizzo toContact");
				Address sipAddress = new Address(name, ip,
						Integer.parseInt(port));
				BasicMessage bmsg = new BasicMessage();
				bmsg.setPayload(new Payload(map));
				// testPeer.send(sipAddress, bmsg);

				// testPeer.send(sipAddress, nonsocheindirizzo, bmsg);
				testPeer.send(sipAddress, bmsg);
				System.out.println("sent to-> " + sipAddress.toString());
				// System.exit(0);
			}

			// String address,name,ip,port;
			// address=args[2];
			// int at=address.indexOf("@");
			// int index=address.indexOf(":");
			// name=address.substring(0,at);
			// ip=address.substring(at+1, index);
			// port=address.substring(index+1);
			// HashMap<String, String> map=new HashMap<String, String>();
			// map.put("IP_DEST", ip);
			// map.put("PORT_DEST", port);
			// Address sipAddress=new Address(name,ip,Integer.parseInt(port));
			// BasicMessage bmsg=new BasicMessage();
			// bmsg.setPayload(new Payload(map));
			// // testPeer.send(sipAddress, bmsg);
			//
			// // testPeer.send(sipAddress, nonsocheindirizzo, bmsg);
			// testPeer.send(sipAddress, bmsg);
			// System.out.println("sent to-> "+sipAddress.toString());
			// // System.exit(0);
			System.out.println("Fine esecuzione main");
		}

	}

}
